package com.hdjk.view.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hdjk.view.R;
import com.hdjk.view.activity.personalcenter.AlertPwdActivity;
import com.hdjk.view.activity.personalcenter.ForgetPwdActivity;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.UserEntity;
import com.hdjk.view.network.Network;
import com.hdjk.view.util.StringAppUtil;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.db.SqliteTools;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 登录页
 */
public class LoginActivity extends BaseActivity implements OnClickListener {

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private EditText et_phone;
    private EditText et_pwd;
    private TextView tv_register;
    private TextView tv_forget;
    private Button btn_login;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        btn_login = (Button) findViewById(R.id.btn_login);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_register:
                intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_forget:
                intent = new Intent(this, ForgetPwdActivity.class);
                intent.putExtra("pwdType", AlertPwdActivity.PWD_TYPE_LOGIN);
                startActivity(intent);
                break;
            case R.id.tvNavBack:
                back();
                break;
        }
    }

    private void login() {
        final String phone = et_phone.getText().toString();
        final String password = et_pwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(LoginActivity.this, "手机号不能为空", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        mDialog.show();
        JSONObject obj = new JSONObject();
        try {
            obj.put("Phone", phone);
            obj.put("DeviceName", "android");
            obj.put("md5Pw", password);
            Network.loginByUsername(obj.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg != null && msg.getSuccess()) {
                        ToastUtils.showShortToast(LoginActivity.this, "登录成功");
                        if (msg.getData() != null && msg.getData() instanceof UserEntity) {
                            UserEntity entity = (UserEntity) msg.getData();
                            // 先储存token
                            UserTokenUtil.userLoginAction(LoginActivity.this, entity.getToken(), StringAppUtil.getMD5(password + entity.getToken()), entity.getLoginName(), entity.getHoldingCode(), entity.getPhone(), password,entity.getId());

                            // 切换手机号后，需要重置单例的DatabaseHelper
                            SqliteTools.getInstance(LoginActivity.this).resetTools(LoginActivity.this);
                        }
//                                // 启动推送服务 设置别名
//                                if (JPushInterface.isPushStopped(LoginActivity.this)) {
//                                    JPushInterface.resumePush(LoginActivity.this);
//                                }
//                                JPushInterface.setAlias(LoginActivity.this, 0, phone);
//
//                                Intent intent = new Intent(LoginActivity.this, MainTabActivity.class);
//                                startActivity(intent);
                        setResult(111);
                        back();

                    } else {
                        if (msg != null && !TextUtils.isEmpty(msg.getMsg()) && !"null".equals(msg.getMsg())) {
                            ToastUtils.showShortToast(LoginActivity.this, msg.getMsg());
                        } else {
                            ToastUtils.showShortToast(LoginActivity.this, "登录失败");
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
